package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.DynamicProblem;
import cn.pugle.oj.catalog.Unknown;

/**
 * 破test case太多了
 * 0
 * 00
 * 2101
 * <p>
 * 错误写法, 虽然这题testcase多, 但是dp的递推思路确实写错了
 *
 * @author tzp
 * @since 2020/10/23
 */
public class LC91_1 implements DynamicProblem {
    public int numDecodings(String s) {
        if (s == null || s.length() == 0 || s.startsWith("0")) return 0;
        int lastlast = 1, last = 1;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i - 1) != '1' && s.charAt(i - 1) != '2' && s.charAt(i) == '0') {
                return 0;
            }
            if (s.charAt(i) == '0') {
                continue;
            } else if (isValid(s, i)) {//递推错误1
                int tmp = last;
                last = lastlast + last;
                lastlast = tmp;
            } else {//递推错误2
                lastlast = last;
            }
        }
        return last;
    }

    public boolean isValid(String s, int i) {
        char a = s.charAt(i - 1);
        char b = s.charAt(i);
        //valid错误12345
        if (a == '0' || a > '2') {
            return false;
        } else if (a == '2' && b > '6') {
            return false;
        } else if (i < s.length() - 1 && s.charAt(i + 1) == '0') {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) {
        System.out.println(new LC91_1().numDecodings("123"));//3
        System.out.println(new LC91_1().numDecodings("2101"));//1
        System.out.println(new LC91_1().numDecodings("1123"));//5
        System.out.println(new LC91_1().numDecodings("10011"));//有够恶心 0
        System.out.println(new LC91_1().numDecodings("123123"));//又递推错了
        System.out.println(new LC91_1().numDecodings("230"));//有够恶心 0
    }
}
